package com.lu.dog.dbrowser.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.lu.adog.util.Logg;

public abstract class BaseDbHelper extends SQLiteOpenHelper {

    public BaseDbHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * 根据相应的表名和列名，删除指定的一行
     *
     * @param tableName
     * @param column
     */
    public void delectARow(String tableName, String column, String value) {
        StringBuilder sb = new StringBuilder();
        sb.append("DELETE FROM ");
        sb.append(tableName);
        sb.append(" WHERE ");
        sb.append(column);
        sb.append("='");
        sb.append(value + "';");
        SQLiteDatabase db = getWritableDatabase();
        Logg.w(">>>",sb.toString());
        db.execSQL(sb.toString());
    }

    /**
     * 删除指定表
     *
     * @param tableName
     */
    public void deleteTable(String tableName) {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(tableName, null, null);
    }

    public Cursor selectAll(String tableName) {
        SQLiteDatabase db = getReadableDatabase();
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM " + tableName + ";");
        return db.rawQuery(sql.toString(), null);
    }

    /**
     * @param tableName
     * @param orderByColumn
     * @return 顺序的结果
     */
    public Cursor selectAll(String tableName, String orderByColumn) {
        return selectAll(tableName, orderByColumn, false);
    }

    /**
     * @param tableName
     * @param orderByColumn
     * @param isDESC        是否逆序
     * @return
     */
    public Cursor selectAll(String tableName, String orderByColumn, boolean isDESC) {
        SQLiteDatabase db = getReadableDatabase();
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM " + tableName + " ORDER BY " + orderByColumn);
        if (isDESC) {
            sql.append(" DESC");
        }
        sql.append(";");
        return db.rawQuery(sql.toString(), null);
    }

    public boolean selectIfHave(String tableName, String colomunName, String colomunValue) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT COUNT(0) FROM " + tableName + " WHERE " + colomunName + "=?");
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(sql.toString(), new String[]{colomunValue});
        if (cursor != null && cursor.getCount() != 0) {
            cursor.moveToFirst();
            if (cursor.getInt(0) > 0) {
                return true;
            }
        }
        return false;

    }

    public int selectRowCount(String tableName) {
        SQLiteDatabase db = getReadableDatabase();
        String sql = "SELECT COUNT(0) from " + tableName + ";";
        Cursor cursor = db.rawQuery(sql, null);

        if (cursor == null) {
            return 0;
        }
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        return count;
    }

    public Cursor selectByColumnVal(String tableName,String columnName,Object val){
        SQLiteDatabase db = getReadableDatabase();
        return db.rawQuery("SELECT * FROM "+ tableName +" WHERE " + columnName + " == "+ val+";",null);
    }
}
